package be.tihh.mgmt.util;

import be.tihh.mgmt.gui.xml.computers.Computer;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;

public class ComputerUtil {


    public static String getIpAddress(Computer computer){

        String address;
        try {
            address = Inet4Address.getByName(computer.getName()).getHostAddress();
        } catch (UnknownHostException e) {
            address = null;
        }
        return address;

    }

    /**
     * remember : when negative, wait 10 seconds to actually retry (specified in "networkaddress.cache.negative.ttl")
     * @param computer
     * @return
     */
    public static boolean isOnline(Computer computer){
        boolean result = false;

        try {
            result = Inet4Address.getByName(computer.getName()).isReachable(3000);
        } catch (IOException e) {
//            e.printStackTrace();
        }

        return result;
    }

    public static void main(String[] args) {
        System.out.println("adres = " + ComputerUtil.getIpAddress(new Computer("Server01"  , "PC0123")));
        System.out.println("adres = " + ComputerUtil.getIpAddress(new Computer("Seeerver01", "PC0123")));
    }

}
